package com.promfy.bughunter.domain;

import com.promfy.bughunter.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.format.DateFormat;

public class ScoreBoard {
	private Canvas canvas;
	private Context context;
	private int screenW;
	private int screenH;
	public final static long TOTAL_TIME = 1* 60 * 1000;
	private static long time;
	private int score;
	private int goodScore;
	private Paint paint;
	private int pressedNumber = 0;
	private int index=100;
	static Bitmap[] scoreIcon = new Bitmap[10];
	static Bitmap colon ;
	
	
	
	public static long getTime() {
		return time;
	}

	public static void setTime(long time) {
		ScoreBoard.time = time;
	}

	public ScoreBoard(Canvas canvas, Context context, int screenW, int screenH) {
		super();
		this.canvas = canvas;  
		this.context = context;
		this.screenW = screenW;
		this.screenH = screenH;
		time = System.currentTimeMillis();
		paint = new Paint();
		paint.setTextSize(30);
		paint.setAntiAlias(true);
		paint.setColor(Color.WHITE);
		
		if(scoreIcon[0]==null)
			scoreIcon[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bug_piaochong_killed_1);
		if(scoreIcon[1]==null)
			scoreIcon[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bug_piaochong_killed_1);
		if(scoreIcon[2]==null)
			scoreIcon[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bug_piaochong_killed_1);
		if(scoreIcon[3]==null)
			scoreIcon[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bug_piaochong_killed_1);
		if(scoreIcon[4]==null)
			scoreIcon[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bug_piaochong_killed_1);
		if(scoreIcon[5]==null)
			scoreIcon[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bug_piaochong_killed_1);
		if(scoreIcon[6]==null)
			scoreIcon[6] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bug_piaochong_killed_1);
		if(scoreIcon[7]==null)
			scoreIcon[7] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bug_piaochong_killed_1);
		if(scoreIcon[8]==null)
			scoreIcon[8] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bug_piaochong_killed_1);
		if(scoreIcon[9]==null)
			scoreIcon[9] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bug_piaochong_killed_1);
		if(colon==null)
			colon = BitmapFactory.decodeResource(context.getResources(), R.drawable.bug_piaochong_killed_1);

  
	}

	/**
	 * add score
	 */
	public void addScore(int value){
		score += value;
//		paint.setTextSize(40);
//		setGoodScore(value);
		
	}
	
	public String getFormatTime() {
		long dur = TOTAL_TIME - (System.currentTimeMillis() - time);
		if(dur <= 0){
			return null;
		}
		DateFormat df = new DateFormat();
		return df.format("mm:ss", dur).toString();
	}

	public int getScore() {   
		return score;
	}

	public int getGoodScore() {
		return goodScore;
	}
	
	public int getPressedNumber() {
		return pressedNumber;
	}

	public void setPressedNumber(int pressedNumber) {
		this.pressedNumber = pressedNumber;
	}

	public int getIndex() {
		if (index > 1000) index = 100;
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public void flushGoodScore(){
		if(getIndex() > 25) return; 
		int value = getGoodScore();
		if(value == 10){
			canvas.drawText("Good! +10", screenW/2-100, screenH/2 -20, paint);
		}else if (value == 20){
			canvas.drawText("Excellent! +20", screenW/2-100, screenH/2 -20, paint);
		}else if (value >= 30){
			canvas.drawText("Perfect! +" + value, screenW/2-100, screenH/2 -20, paint);			
		}
		addIndex();
	}

	private void addIndex() {
		index++;
	}

	public void setGoodScore(int goodScore) {
		score += goodScore;
		this.goodScore = goodScore;
	}

	/**
	 * flush score board in time;
	 * if timeout, return false;
	 */
	public boolean runScoreBoardInTime(){
		String formatTime = getFormatTime();
		if(formatTime == null) return false;
//		canvas.drawText(formatTime, 20, 50, paint);
//		canvas.drawText(getScore()+"", screenW -80, 50, paint);
		drawTime(formatTime);
		drawScore(getScore());
		flushGoodScore();
		return true;
	}


	
	public void addPressedNumeber() {
		pressedNumber++;
	}
	
	/**
	 * draw icon of score
	 * @param score
	 */
	public void drawScore(int score){
		String scoreStr = score + "";
		float left = screenW - 50 ;
		float top = 50;
		char[] chars = scoreStr.toCharArray();
		Bitmap b;
		for(int i=0,j=chars.length-1; j>=0; i++,j--){
			b = getMappedBitmap(String.valueOf(chars[j]));
			canvas.drawBitmap(b, left - (b.getWidth() + b.getWidth()*i), top, paint);
		}
	}
	
	/**
	 * draw time 
	 * format: mm:ss
	 * @param time
	 */
	public void drawTime(String time){
		float left = 50 ;
		float top = 50;
		char[] chars = time.toCharArray();
		Bitmap b;
		for(int i=0; i < chars.length; i++){
			b = getMappedBitmap(String.valueOf(chars[i]));
			canvas.drawBitmap(b, left +  b.getWidth()*i, top, paint);
		}
	}
	
	/**
	 * mapped number-string to number-icons.
	 * @param number
	 * @return
	 */
	private Bitmap getMappedBitmap(String number){
		if("0".equals(number)){
			return scoreIcon[0];
		}
		else if("1".equals(number)){
			return scoreIcon[1];
		}
		else if("2".equals(number)){
			return scoreIcon[2];
		}
		else if("3".equals(number)){
			return scoreIcon[3];
		}
		else if("4".equals(number)){
			return scoreIcon[4];
		}
		else if("5".equals(number)){
			return scoreIcon[5];
		}
		else if("6".equals(number)){
			return scoreIcon[6];
		}
		else if("7".equals(number)){
			return scoreIcon[7];
		}
		else if("8".equals(number)){
			return scoreIcon[8];
		}
		else if("9".equals(number)){
			return scoreIcon[9];
		}else if(":".equals(number)){
			return colon;
		}
		return null;
	}
	

}
